package com.easy.auth.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easy.auth.domain.ErrorLog;
import com.easy.auth.service.ErrorLogService;
import com.easy.common.jdbc.PageInfo;
import com.easy.common.util.DateUtil;
import com.easy.common.util.FileUtil;
import com.easy.common.util.Parameter;
import com.easy.common.web.FilterUtil;
import com.easy.common.web.PagedList;
import com.easy.common.web.message.DeleteMessage;
import com.easy.common.web.message.Message;
import com.easy.common.web.message.SaveOrEditMessage;

@Controller
@RequestMapping("ErrorLogController")
public class ErrorLogController {

 
	@Autowired
	private ErrorLogService ErrorLogService;

//	@Autowired
//	private ImpErrorLogService impErrorLogService;
//
//	@Autowired
//	private ErrorLoglogService ErrorLoglogService;
	 
	/**
	 * 首页
	 * @param request
	 * @return
	 */

	
 
	 
 
	 
	@RequestMapping("errorLog")
	public String errorLog(HttpServletRequest request,String secondMenuId){
		request.getSession().setAttribute("secondMenuId", secondMenuId);
		return "DPV/errorLog";	
	}


	public ErrorLog fileToErrorLog(File nasFile,File localFile){
		ErrorLog ErrorLog = new ErrorLog();
		ErrorLog.setOid("");
		Date now=new Date();
		String time="";
		//ErrorLog.setCreater(creater);
		//ErrorLog.setId_path(localFile);
		ErrorLog.setIsfile(nasFile.isFile()+"");
		ErrorLog.setIsroot(nasFile.isFile()+"");
		//ErrorLog.setLevel(level);
		ErrorLog.setLocal_name(localFile.getName());
		ErrorLog.setLocal_path(localFile.getPath());
		ErrorLog.setNas_path(nasFile.getPath());
		ErrorLog.setOname(nasFile.getName());
		//ErrorLog.setFile_time(nasFile.lastModified()+"");//文件最后修改时间
		//ErrorLog.setPid(pid);
		ErrorLog.setState("1");
		//ErrorLog.set
		//ErrorLog.set
		
		return ErrorLog;
	}
	
	/**
	 * 查看pdf
	 * @param request
	 * @return
	 */
	
	@RequestMapping("pdf")
	public String pdf(HttpServletRequest request){
		String url=request.getParameter("url");
		request.getSession().setAttribute("url", url);
		return "DPV/pdf";	
	}
	@RequestMapping("vsd")
	public String vsd(HttpServletRequest request){
		String url=request.getParameter("url");
		String fileName = request.getParameter("fileName");
		request.getSession().setAttribute("url", url);
		request.getSession().setAttribute("fileName", fileName);
		return "DPV/fileDownLoad";	
	}
		
	@RequestMapping("gotoDPV1")
	public String gotoDPV1(HttpServletRequest request) {
		return "DPV/ErrorLogList";
	}
	@RequestMapping("gotoDPV2")
	public String gotoDPV2(HttpServletRequest request) {
		return "DPV/ErrorLogList";
	}
	

	
		@RequestMapping("goToErrorLogManager")
		public String goToErrorLogManager(String secondMenuId,HttpServletRequest request) {
			request.getSession().setAttribute("secondMenuId", secondMenuId);
			return "";
		}

		/**
		 * 
		 * @param oid
		 * @return
		 */
		@RequestMapping("getSonErrorLogTree")
		@ResponseBody
		public String getSonErrorLogTree(String oid) {
			//System.out.println("---------------"+oid);
			List<ErrorLog> ErrorLogList=ErrorLogService.getSonErrorLogTree(oid);
			JSONArray jsonArray = net.sf.json.JSONArray.fromObject(ErrorLogList);
			return jsonArray.toString();
		}
		/**
		 * 查询报告树
		 * 
		 * @param param  分页及过滤参数
		 * @return 列表信息集合
		 */
		@RequestMapping("getErrorLogTree")
		@ResponseBody
		public String getErrorLogsTree(String oid) {
			PageInfo param=new PageInfo();
			param.setRows(9999);
			ErrorLog ErrorLogTree = (ErrorLog) FilterUtil.getVO(ErrorLog.class,
					param.getFilterRules());
			//ErrorLogTree.setIsfile("");
			// todo
			PagedList<ErrorLog> pageErrorLog=ErrorLogService.findAllErrorLogEqual(ErrorLogTree, param);
			JSONArray jsonArray = net.sf.json.JSONArray.fromObject(pageErrorLog.getRows());
			return jsonArray.toString();
		}
		
		
		
		@RequestMapping("getAllErrorLog")
		@ResponseBody
		public PagedList<ErrorLog> getAllErrorLogs(PageInfo param) {
			ErrorLog ErrorLogTree = (ErrorLog) FilterUtil.getVO(ErrorLog.class,
					param.getFilterRules());
			return ErrorLogService.findAllErrorLogEqual(ErrorLogTree, param);
		}
		
		/**
		 * 不用了
		 * @return
		 */
		@RequestMapping("getNewErrorLogs")
		@ResponseBody
		public List<ErrorLog> getNewErrorLogs() {
			PageInfo param=new PageInfo();
			ErrorLog ErrorLog = (ErrorLog) FilterUtil.getVO(ErrorLog.class,
					param.getFilterRules());
			ErrorLog.setLevel("7");
			param.setSort("last_modify_time");
			param.setRows(8);
			return ErrorLogService.findAllErrorLogEqual(ErrorLog, param).getRows();
		}
		
		
		
		@RequestMapping("getErrorLogForGrid")
		@ResponseBody
		public PagedList<ErrorLog> getErrorLogForGrid(PageInfo param,ErrorLog ErrorLog) {
			ErrorLog searchErrorLog = (ErrorLog) FilterUtil.getVO(ErrorLog.class,
					param.getFilterRules());
			searchErrorLog.setIsfile("true");
			searchErrorLog.setPid(ErrorLog.getPid());
			searchErrorLog.setFile_create_time(ErrorLog.getFile_create_time());
//			searchErrorLog.setPid(ErrorLog.getPid());
//			searchErrorLog.setFile_time(ErrorLog.getFile_time());
//			System.out.println("``````````````````"+ErrorLog.getPid());
//			System.out.println("``````````````````"+ErrorLog.getFile_create_time());
//			System.out.println("``````````````````"+param.getFilterRules());
			return ErrorLogService.findAllErrorLogLikeAndEqualPid(searchErrorLog, param);
		}

		@RequestMapping("getErrorLogsByTime")
		@ResponseBody
		public List<ErrorLog> getErrorLogsByTime(String serchTime,String path,String fileName,String time){
			PagedList<ErrorLog> ErrorLogPageList=new PagedList<ErrorLog>();
			List<ErrorLog> ErrorLogList=new ArrayList<ErrorLog>();// 
			ErrorLog ErrorLog = new ErrorLog();
			ErrorLog.setLocal_path(path);
			ErrorLog.setOname(fileName);
			ErrorLog.setFile_create_time(time);
			ErrorLogList.add(ErrorLog);
			//ErrorLogPageList.setRows(ErrorLogList);
			return ErrorLogList ;

		}
		
		
		/**
		 * 删除指定报告
		 * 
		 * @param oids   删除报告的id集合
		 * @return Message
		 */
		@RequestMapping("removeErrorLog")
		@ResponseBody
		public Message removeErrorLog(String ids) {
			return new DeleteMessage(ErrorLogService.removeErrorLog(ids));
		}

		/**
		 * 新增或编辑报告
		 * 
		 * @param ErrorLogTree
		 * @return
		 */
		@RequestMapping("saveOrEditErrorLog")
		@ResponseBody
		public Message saveOrEditErrorLog(ErrorLog ErrorLog) {
			return new SaveOrEditMessage(
					ErrorLogService.saveOrEditErrorLog(ErrorLog));
		}

		/**
		 * 编辑报告时查询选择的报告信息
		 * 
		 * @param ErrorLogTree
		 * @return
		 */
		@RequestMapping("findErrorLog")
		@ResponseBody
		public List<ErrorLog> findErrorLog(String pid) {
			ErrorLog ErrorLog=new ErrorLog();
//			if(pid==null||pid.equals("")){
//				ErrorLog.setLevel("2");
//			}else{
//				ErrorLog.setPid(pid);
//			}
		
			ErrorLog.setPid(pid);
			//System.out.println("````````````````"+ErrorLog.getLevel());
			return ErrorLogService.findAllErrorLogEqual(ErrorLog, new PageInfo()).getRows();
			//return ErrorLogService.findErrorLog(ErrorLog);
		}

 
		 

}
